package ua.com.softway.db.batch;

import java.util.List;

import org.hibernate.Criteria;

public class LimitOffsetBatcher implements Batcher {
    private final int batchSize;

    public LimitOffsetBatcher(int batchSize) {
        this.batchSize = batchSize;
    }
    
    private int currentBatchPosition;
    
    @Override
    @SuppressWarnings("unchecked")
    public List<Object> nextBatch(Criteria criteria) {
        criteria.setFirstResult(currentBatchPosition);
        criteria.setMaxResults(batchSize);
        
        List<Object> list = criteria.list();
        
        if (list.isEmpty()) return null;
        
        currentBatchPosition = currentBatchPosition + batchSize;
        
        return list;
    }
}
